package com.block.common.util;

import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.core.util.HexUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.symmetric.AES;

public class AESUtil {

    private static final String MODE = "CBC";
    private static final String PADDING = "PKCS7Padding";
    private static final String IV = "0392039203920300";

    /**
     * aes 加密后用 base64编码
     * @param sSrc
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String encryptBase64(String sSrc,String sKey) throws Exception{
        try {
            AES aes = new AES(MODE, PADDING,
                    // 密钥，可以自定义
                    sKey.getBytes(),
                    // iv加盐，按照实际需求添加
                    IV.getBytes());
            String decryptStr = aes.encryptBase64(sSrc);
            return decryptStr;
        } catch (Exception ex) {
            throw ex;
        }
    }

    public static String encryptBase64(String sSrc,String sKey, String iv){
        AES aes = new AES(MODE, PADDING,
                // 密钥，可以自定义
                sKey.getBytes(),
                // iv加盐，按照实际需求添加
                iv.getBytes());
        String decryptStr = aes.encryptBase64(sSrc);
        return decryptStr;
    }

    /**
     * 解密前，先用base64解码
     * @param sSrc
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String decryptBase64(String sSrc,String sKey) throws Exception{
        try {
            AES aes = new AES(MODE, PADDING,
                    // 密钥，可以自定义
                    sKey.getBytes(),
                    // iv加盐，按照实际需求添加
                    IV.getBytes());
            String decryptStr = aes.decryptStr(Base64Decoder.decode(sSrc));
            return decryptStr;
        } catch (Exception ex) {
            throw ex;
        }
    }

    public static String decryptBase64(String sSrc,String sKey, String iv) throws Exception{
        try {
            AES aes = new AES(MODE, PADDING,
                    // 密钥，可以自定义
                    sKey.getBytes(),
                    // iv加盐，按照实际需求添加
                    iv.getBytes());
            String decryptStr = aes.decryptStr(Base64Decoder.decode(sSrc));
            return decryptStr;
        } catch (Exception ex) {
            throw ex;
        }
    }

    /**
     * aes 加密后用 base64编码
     * @param sSrc
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String encryptHex(String sSrc,String sKey) throws Exception{
        try {
            AES aes = new AES(MODE, PADDING,
                    // 密钥，可以自定义
                    sKey.getBytes(),
                    // iv加盐，按照实际需求添加
                    IV.getBytes());
            String decryptStr = aes.encryptHex(sSrc);
            return decryptStr;
        } catch (Exception ex) {
            throw ex;
        }
    }


    /**
     * 解密前，先用base64解码
     * @param sSrc
     * @param sKey
     * @return
     * @throws Exception
     */
    public static String decryptHex(String sSrc,String sKey) throws Exception{
        try {
            AES aes = new AES(MODE, PADDING,
                    // 密钥，可以自定义
                    sKey.getBytes(),
                    // iv加盐，按照实际需求添加
                    IV.getBytes());
            String decryptStr = aes.decryptStr(HexUtil.decodeHex(sSrc));
            return decryptStr;
        } catch (Exception ex) {
            throw ex;
        }
    }


    public static void main(String[] args) throws Exception {

        String content = "1111";

        String randKey = "f66h5r1cErozsBepDnJI5enkyybPxlWy";

        String encode = encryptBase64(content, randKey);
        System.out.println(encode);
        encode = decryptBase64(encode, randKey);

        System.out.println(encode);
    }

}
